<?php
/**
 * Created by mww
 * User: mww
 */

namespace app\admin\controller;
use think\Db;
use think\Request;

class Company extends Base
{
    /**
     * 首页
     */
    public function index(){
        $com_str = '';
        if(session("company_id") > 0){
            $com_info = Db::table('up_company')->where('c_id', session("company_id"))
                ->field('c_verify,c_verify_remark')
                ->find();
            if($com_info['c_verify'] == 1){
                $com_str = '企业信息审核中~';
            }
            if($com_info['c_verify'] == 3){
                $com_str = '企业信息修改被拒，拒绝原因：' . $com_info['c_verify_remark'];
            }
        }
        if(request()->isAjax()){
            extract(input());
            $where = [
                'c_status' => ['<>',3]
            ];
            if(session("company_id") > 0){
                $where['c_id'] = session("company_id");
            }
            if(isset($user_name) && $user_name != ''){
                $where['c_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($c_verify) && $c_verify != ''){
                $where['c_verify'] = $c_verify;
            }
            if(isset($c_status) && $c_status != ''){
                $where['c_status'] = $c_status;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_company')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_company')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('c_status asc,c_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    $lists[$k]['teams'] = Db::table('up_company_team')
                        ->where('ct_c_id',$v['c_id'])
                        ->where('ct_status','<>',3)
                        ->count();
                    if(session("company_id") > 0){
                        if($v['c_verify'] == 1){
                            $lists[$k]['c_name'] = $v['c_name2'];
                            $lists[$k]['c_code'] = $v['c_code2'];
                            $lists[$k]['c_zhizhao'] = $v['c_zhizhao2'];
                        }
                    }

                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists, 'com_str' => $com_str]);
        }
        $this->assign('com_str',$com_str);
        return $this->fetch();
    }
    /**
     * 企业状态
     */
    public function comStatus()
    {
        extract(input());
        $res = Db::table('up_company')->where('c_id',$id)->setField('c_status',$num);
        if($res){
            Db::table('up_admin')->where('company_id',$id)->setField('status',$num);
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 公司详情
     * @return mixed
     */
    public function dynDetail(){
        $c_id = input('c_id');
        $info = Db::table('up_company')
            ->where('c_id',$c_id)
            ->find();
        $vv = [
            1 => '认证中',
            2 => '已通过',
            3 => '已拒绝',
        ];
        $this->assign('info', $info);
        $this->assign('vv', $vv);
        return $this->fetch('company/dyndetail');
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function dynDel(){
        $c_id = input('id');
        $res = Db::table('up_company')->where('c_id',$c_id)->setField('c_status',3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function addCom(){
        if(Request::instance()->isPost()){
            extract(input());
            $have = Db::table('up_company')
                ->where('c_username', $c_username)
                ->field('c_username')
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '已有此账号！']);
            }
            //--------------------------------------------
            $have = Db::table('up_admin')
                ->where('username', $c_username)
                ->field('username')
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '已有此账号！']);
            }
            //-----------------------------------------------------
            $today = date('Y-m-d H:i:s');
            $ss = Db::table('up_area')
                ->where('district_id','in',"$c_sheng,$c_shi,$c_qu")
                ->column('district');
            $regex = '/^[\x{4e00}-\x{9fa5}A-Za-z0-9]{1,100}$/u';
            $regex2 = "/^[a-zA-Z0-9]+$/";
            if (!$c_name) {
                return json(['code' => 100, 'msg' => '名字不能为空']);
            }
            if (!preg_match($regex2,$c_code)) {
                return json(['code' => 100, 'msg' => '信用代码只能输入字母、数字']);
            }
            $insert = [
                'c_name' => $c_name,
                'c_name2' => $c_name,
                'c_logo' => $c_logo,
                'c_charge_name' => $c_charge_name,
                'c_charge_phone' => $c_charge_phone,
                'c_sheng' => $c_sheng,
                'c_shi' => $c_shi,
                'c_qu' => $c_qu,
                'c_area_str' => implode($ss,','),
                'c_address' => $c_address,
                'c_faren' => $c_faren,
                'c_code' => $c_code,
                'c_code2' => $c_code,
                'c_zhizhao' => $c_zhizhao,
                'c_zhizhao2' => $c_zhizhao,
                'c_username' => $c_username,
                'c_password' => md5(md5($c_password) . config('auth_key')),
                'c_content' => $c_content,
                'c_verify' => 2,
                'c_can' => 2,
                'c_time' => $today,
                'c_verifytime' => $today,
                'c_updatetime' => $today,
            ];
            Db::startTrans();// 启动事务
            try{
                $cid = Db::table('up_company')->insertGetId($insert);
                $adminid = Db::table('up_admin')->insertGetId([
                    'username' => $insert['c_username'],
                    'password' => $insert['c_password'],
                    'status' => 1,
                    'groupid' => 5,
                    'create_time' => time(),
                    'company_id' => $cid,
                ]);
                Db::name('auth_group_access')->insert(['uid'=> $adminid,'group_id'=>5]);
                Db::commit();// 提交事务
                return json(['code' => 200, 'msg' => '添加成功']);
            }catch( \Exception $e){
                Db::rollback ();//回滚事务
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch('company/add');
    }
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function comEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = Db::table('up_company')->where('c_id', $c_id)
                ->field('c_username,c_name,c_code,c_zhizhao')
                ->find();
            if(session("company_id") == 0) {
                if ($data['c_username'] != $c_username) {
                    $have = Db::table('up_company')
                        ->where('c_id', '<>', $c_id)
                        ->where('c_username', $c_username)
                        ->field('c_username')
                        ->find();
                    if ($have) {
                        return json(['code' => 100, 'msg' => '已有此账号！']);
                    }
                }
                //--------------------------------------------
                $have = Db::table('up_admin')
                    ->where('username', $c_username)
                    ->where('company_id', '<>', $c_id)
                    ->field('username')
                    ->find();
                if ($have) {
                    return json(['code' => 100, 'msg' => '已有此账号！']);
                }
                //-----------------------------------------------------
            }
            $ss = Db::table('up_area')
                ->where('district_id','in',"$c_sheng,$c_shi,$c_qu")
                ->column('district');
            $regex = '/^[\x{4e00}-\x{9fa5}A-Za-z0-9]{1,100}$/u';
            $regex2 = "/^[a-zA-Z0-9]+$/";
            if (!$c_name) {
                return json(['code' => 100, 'msg' => '名字不能为空']);
            }
            if (!preg_match($regex2,$c_code)) {
                return json(['code' => 100, 'msg' => '信用代码只能输入字母、数字']);
            }
            $update = [
                'c_name2' => $c_name,
                'c_logo' => $c_logo,
                'c_charge_name' => $c_charge_name,
                'c_charge_phone' => $c_charge_phone,
                'c_sheng' => $c_sheng,
                'c_shi' => $c_shi,
                'c_qu' => $c_qu,
                'c_area_str' => implode($ss,','),
                'c_address' => $c_address,
                'c_faren' => $c_faren,
                'c_code2' => $c_code,
                'c_zhizhao2' => $c_zhizhao,
                'c_content' => $c_content,
                'c_updatetime' => date('Y-m-d H:i:s')
            ];
            if($data['c_name'] != $c_name || $data['c_code'] != $c_code || $data['c_zhizhao'] != $c_zhizhao){
                if(session("company_id") == 0) {
                    $update['c_name'] = $c_name;
                    $update['c_code'] = $c_code;
                    $update['c_zhizhao'] = $c_zhizhao;
                }else{
                    $update['c_verify'] = 1;
                }
            }
            Db::startTrans();// 启动事务
            try{
                if(session("company_id") == 0) {
                    $update['c_username'] = $c_username;
                    $updateadmin = [
                        'username' => $c_username,
                        'update_time' => time()
                    ];
                    if ($c_password) {
                        $update['c_password'] = md5(md5($c_password) . config('auth_key'));
                        $updateadmin['password'] = md5(md5($c_password) . config('auth_key'));
                    }
                    Db::table('up_admin')->where('company_id',$c_id)->update($updateadmin);
                }
                Db::table('up_company')->where('c_id', $c_id)->update($update);
                Db::commit();// 提交事务
                return json(['code' => 200, 'msg' => '编辑成功']);
            }catch( \Exception $e){
                Db::rollback ();//回滚事务
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $c_id = input('c_id');
        $info = Db::table('up_company')
            ->where('c_id',$c_id)
            ->find();
        //
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        // 市
        $city = Db::table('up_area')
            ->where('level', 2)
            ->where('pid', $info['c_sheng'])
            ->field('district_id, district')
            ->select();
        // 区
        $district = Db::table('up_area')
            ->where('level', 3)
            ->where('pid', $info['c_shi'])
            ->field('district_id, district')
            ->select();
        $this->assign([
            'info' => $info,
            'province' => $province,
            'city' => $city,
            'district' => $district
        ]);
        return $this->fetch('company/edit');
    }
    //-------------------------------------团-队-------------------------------------------
    /**
     * 查看团队数
     * @return mixed|\think\response\Json
     */
    public function seeTeams(){
        if(request()->isAjax()){
            extract(input());
            $where['ct_c_id'] = session('c_id');
            $where['ct_fid'] = session('fid');
            $where['ct_status'] = ['<>',3];
            if(isset($user_name) && $user_name != ''){
                $where['ct_name|ct_charge_name|ct_charge_phone'] = ['like', '%' . $user_name . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_company_team')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_company_team')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('ct_status asc,ct_update desc')
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    $ctids = Db::table('up_company_team')
                        ->where('ct_fid_str','like','%' . $v['ct_fid_str'] . '%')
                        ->column('ct_id');
                    $users = Db::table('up_u_user')
                        ->where('user_type',2)
                        ->where('user_room','in',$ctids)
                        ->where('user_status','<',3)
                        ->count();
                    $lists[$k]['users'] = $users;
                    $lists[$k]['teams'] = count($ctids) - 1;

                    $lists[$k]['haveUser'] = 1;//没有用户
                    if($users > 0){
                        $lists[$k]['haveUser'] = 2;
                    }
                    $lists[$k]['haveTeam'] = 1;//没有团队
                    $have = Db::table('up_company_team')->where('ct_fid',$v['ct_id'])->field('ct_id')->find();
                    if($have){
                        $lists[$k]['haveTeam'] = 2;
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $c_id = input('c_id');
        $fid = input('fid');
        session('c_id', $c_id);
        session('fid', $fid);
        return $this->fetch('company/teams');
    }
    
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function downTeam(){
        $ct_id = input('id');
        $datatsr = Db::table('up_company_team')->where('ct_id',$ct_id)->value('ct_fid_str');
        $ctids = Db::table('up_company_team')
            ->where('ct_fid_str','like','%' . $datatsr . '%')
            ->column('ct_id');
        $users = Db::table('up_u_user')
            ->where('user_type',2)
            ->where('user_room','in',$ctids)
            ->where('user_status','<',3)
            ->count();
        if($users >= 1){
            return json(['code' => 100, 'msg' => '此团队下还有成员！']);
        }
        $res = Db::table('up_company_team')->where('ct_fid_str','like','%' . $datatsr . '%')->setField('ct_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '冻结成功']);
        }else{
            return json(['code' => 100, 'msg' => '冻结失败']);
        }
    }
    /**
     * 启用
     * @return \think\response\Json
     */
    public function upTeam(){
        $ct_id = input('id');
        $datatsr = Db::table('up_company_team')->where('ct_id',$ct_id)->value('ct_fid_str');
        $res = Db::table('up_company_team')->where('ct_fid_str','like','%' . $datatsr . '%')->setField('ct_status',1);
        if($res){
            return json(['code' => 200, 'msg' => '启用成功']);
        }else{
            return json(['code' => 100, 'msg' => '启用失败']);
        }
    }
    /**
     * 删除团队
     * @return \think\response\Json
     */
    public function teamDel(){
        $ct_id = input('id');
        $datatsr = Db::table('up_company_team')->where('ct_id',$ct_id)->value('ct_fid_str');
        $ctids = Db::table('up_company_team')
            ->where('ct_fid_str','like','%' . $datatsr . '%')
            ->column('ct_id');
        $users = Db::table('up_u_user')
            ->where('user_type',2)
            ->where('user_room','in',$ctids)
            ->where('user_status','<',3)
            ->count();
        if($users >= 1){
            return json(['code' => 100, 'msg' => '此团队下还有成员！']);
        }
        $res = Db::table('up_company_team')->where('ct_fid_str','like','%' . $datatsr . '%')->setField('ct_status',3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 添加团队
     * @return mixed|\think\response\Json
     */
    public function addTeam(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $insert = [
                'ct_c_id' => session('c_id'),
                'ct_fid' => session('fid'),
                'ct_name' => $ct_name,
                'ct_charge_name' => $ct_charge_name,
                'ct_charge_phone' => $ct_charge_phone,
                'ct_time' => $today,
                'ct_update' => $today,
            ];
            Db::startTrans();
            try {
                $resid = Db::table('up_company_team')->insertGetId($insert);
                if($insert['ct_fid'] <= 0){
                    $fidstr = ',' . $resid . ',';
                    $ct_level = 1;
                }else{
                    $fstr = Db::table('up_company_team')->where('ct_id',$insert['ct_fid'])->value('ct_fid_str');
                    $fidstr = $fstr . $resid . ',';
                    $ct_level = count(explode(',',trim($fidstr,',')));
                }
                Db::table('up_company_team')->where('ct_id',$resid)->update([
                    'ct_fid_str' => $fidstr,
                    'ct_level' => $ct_level
                ]);
                Db::commit();
                return json(['code' => 200, 'msg' => '添加成功']);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('company/add_team');
    }
    /**
     * 编辑团队
     * @return mixed|\think\response\Json
     */
    public function editTeam(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $update = [
                'ct_name' => $ct_name,
                'ct_charge_name' => $ct_charge_name,
                'ct_charge_phone' => $ct_charge_phone,
                'ct_update' => $today,
            ];
            $res = Db::table('up_company_team')->where('ct_id', $ct_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $ct_id = input('ct_id');
        $info = Db::table('up_company_team')
            ->where('ct_id',$ct_id)
            ->find();
        $this->assign([
            'info' => $info,
        ]);
        return $this->fetch('company/edit_team');
    }

    //-------------------------------------团-队-成-员-------------------------------------------
    /**
     * 查看团队成员
     * @return mixed|\think\response\Json
     */
    public function seeTeamUsers(){
        if(request()->isAjax()){
            extract(input());
            $where['user_room'] = session('ct_id');
            $where['user_type'] = 2;
            $where['user_status'] = ['<',3];
            if(isset($user_name) && $user_name != ''){
                $where['user_username|user_phone|user_job_desc'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($user_status) && $user_status != ''){
                $where['user_status'] = $user_status;
            }
            if(isset($user_auth) && $user_auth != ''){
                $where['user_auth'] = $user_auth;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_u_user')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('user_status asc,user_updatetime desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $ct_id = input('ct_id');
        session('ct_id', $ct_id);
        return $this->fetch('company/users');
    }

    /**
     * 冻结
     * @return \think\response\Json
     */
    public function downTeamuser(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id',$user_id)->setField('user_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '冻结成功']);
        }else{
            return json(['code' => 100, 'msg' => '冻结失败']);
        }
    }
    /**
     * 启用
     * @return \think\response\Json
     */
    public function upTeamuser(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id',$user_id)->setField('user_status',1);
        if($res){
            return json(['code' => 200, 'msg' => '启用成功']);
        }else{
            return json(['code' => 100, 'msg' => '启用失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function teamDeluser(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id',$user_id)->setField('user_status',3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function addTeamuser(){
        if(Request::instance()->isPost()){
            extract(input());
            if(preg_match("/^1[123456789]\d{9}$/", $user_phone)) {
                if(mb_strlen($user_password) < 6){
                    return json(['code' => 100, 'msg' => '密码不能低于6位']);
                }
                $have = Db::table('up_u_user')
                    ->where('user_phone', $user_phone)
                    ->field('user_id')
                    ->find();
                if($have){
                    return json(['code' => 100, 'msg' => '已有此手机号！']);
                }
                $c_id = Db::table('up_company_team')->where('ct_id', session('ct_id'))->value('ct_c_id');
                $today = date('Y-m-d H:i:s');
                $insert = [
                    'user_username' => $user_username,
                    'user_sex' => $user_sex,
                    'user_birthday' => $user_birthday,
                    'user_phone' => $user_phone,
                    'user_hospital' => $c_id,
                    'user_room' => session('ct_id'),
                    'user_job_desc' => $user_job_desc,
                    'user_password' => md5($user_password),
                    'user_type' => 2,
                    'user_auth' => $user_auth,
                    'user_verify' => 2,
                    'user_time' => $today,
                    'user_updatetime' => $today,
                ];
                $res = Db::table('up_u_user')->insert($insert);
                if ($res) {
                    return json(['code' => 200, 'msg' => '添加成功']);
                } else {
                    return json(['code' => 100, 'msg' => '添加失败']);
                }
            }else {
                return json(['code' => 100, 'msg' => '请填写正确的手机号码']);
            }
        }
        return $this->fetch('company/add_user');
    }
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function editTeamuser(){
        if(Request::instance()->isPost()){
            extract(input());
            if(preg_match("/^1[123456789]\d{9}$/", $user_phone)) {
                $have = Db::table('up_u_user')
                    ->where('user_id', '<>', $user_id)
                    ->where('user_phone', $user_phone)
                    ->field('user_id')
                    ->find();
                if ($have) {
                    return json(['code' => 100, 'msg' => '已有此手机号！']);
                }
                $today = date('Y-m-d H:i:s');
                $update = [
                    'user_username' => $user_username,
                    'user_sex' => $user_sex,
                    'user_birthday' => $user_birthday,
                    'user_phone' => $user_phone,
                    'user_job_desc' => $user_job_desc,
                    'user_auth' => $user_auth,
                    'user_updatetime' => $today,
                ];
                if($user_password){
                    if (mb_strlen($user_password) < 6) {
                        return json(['code' => 100, 'msg' => '密码不能低于6位']);
                    }
                    $update['user_password'] = md5($user_password);
                }
                $res = Db::table('up_u_user')->where('user_id', $user_id)->update($update);
                if ($res) {
                    return json(['code' => 200, 'msg' => '编辑成功']);
                } else {
                    return json(['code' => 100, 'msg' => '编辑失败']);
                }
            }else {
                return json(['code' => 100, 'msg' => '手机号码有误']);
            }
        }
        $user_id = input('user_id');
        $info = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->find();
        $this->assign([
            'info' => $info,
        ]);
        return $this->fetch('company/edit_user');
    }
    /**
     * 转移团队
     * @return mixed|\think\response\Json
     */
    public function editTeamFor(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $update = [
                'user_room' => $user_room,
                'user_updatetime' => $today,
            ];
            $res = Db::table('up_u_user')->where('user_id', 'in',$user_id)->update($update);
            if ($res) {
                return json(['code' => 200, 'msg' => '操作成功']);
            } else {
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $user_id = input('user_id');
        $c_id = input('cid');
        if(!$c_id || empty($c_id)){
            $c_id = session('c_id');
        }
        $teams = Db::table('up_company_team')
            ->where('ct_c_id', $c_id)
//            ->where('ct_fid', 0)
            ->where('ct_status', 1)
            ->select();
//        $data = [];
//        if($teams){
//            foreach ($teams as $k => $v){
//                $son = Db::table('up_company_team')
//                    ->where('ct_c_id', $c_id)
//                    ->where('ct_fid_str', 'like','%' . $v['ct_fid_str'] . '%')
//                    ->where('ct_status', 1)
//                    ->order('ct_level asc')
//                    ->select();
//                $data = array_merge($data,$son);
//            }
//        }
        $data = $this->getTeamTreeDatas($ct_fid=0,$teams);
        $this->assign([
            'user_id' => $user_id,
            'data' => $data
        ]);
        return $this->fetch('company/teamfor');
    }
    public function getTeamTreeDatas($ct_fid=0,$data){
        static $list=array();
        foreach ($data as $k => $v){
            if($v['ct_fid']==$ct_fid){
                /*将该类别的数据放入list中*/
                $list[]=$v;
                $this->getTeamTreeDatas($v['ct_id'],$data);
            }
        }
        return $list;
    }
    /**
     * 导入Excel
     * @return mixed
     */
    public function excelin(){
        set_time_limit(0);
        if(request()->file('file')){
            $file = request()->file('file');
            $info  = $file->getInfo();
            $file_path = $info['tmp_name'];
            //取出文件后缀
            $file_type = explode(".",$info['name']);
            $file_type = end($file_type);
            vendor("PHPExcel.PHPExcel.IOFactory");
            if( $file_type == 'xls' ){
                $inputFileType = 'Excel5';    //这个是读 xls的
            }else{
                $inputFileType = 'Excel2007';//这个是计xlsx的
            }
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file_path);
            $objWorksheet = $objPHPExcel->getActiveSheet();
            $highestRow = $objWorksheet->getHighestRow();
//            print_r($highestRow);die;
            $time = date('Y-m-d H:i:s');
            $data = [];
            for ($i=2;$i<=$highestRow;$i++) {
                $name = $objPHPExcel->getActiveSheet ()->getCell ("A" . $i)->getValue ();//姓名
                $sex = $objPHPExcel->getActiveSheet ()->getCell ("B" . $i)->getValue ();//性别
                $birthday = $objPHPExcel->getActiveSheet ()->getCell ("C" . $i)->getValue ();//出生日期
                $phone = $objPHPExcel->getActiveSheet ()->getCell ("D" . $i)->getValue ();//手机号
                $pass = $objPHPExcel->getActiveSheet ()->getCell ("E" . $i)->getValue ();//登录密码
                $desc = $objPHPExcel->getActiveSheet ()->getCell ("F" . $i)->getValue ();//简介
                $roletype = $objPHPExcel->getActiveSheet ()->getCell ("G" . $i)->getValue ();//角色
                if($phone != '' && $name != '' && (int)$sex > 0 && $pass != '' && (int)$roletype > 0){
                    $sex = (int)$sex;
                    $roletype = (int)$roletype;
                    if(preg_match("/^1[123456789]\d{9}$/", $phone)){
                        $have = Db::table('up_u_user')
                            ->where('user_phone', $phone)
                            ->field('user_id')
                            ->find();
                        if($have){
                            return json(['code' => 100, 'msg' => '{$name}人员的手机号已存在！']);
                        }else{
                            $insert = [
                                'user_username' => $name,
                                'user_sex' => $sex,
                                'user_birthday' => $birthday,
                                'user_phone' => $phone,
                                'user_hospital' => session('c_id'),
                                'user_room' => session('ct_id'),
                                'user_job_desc' => $desc,
                                'user_password' => md5($pass),
                                'user_type' => 2,
                                'user_auth' => $roletype,
                                'user_verify' => 2,
                                'user_time' => $time,
                                'user_updatetime' => $time,
                            ];
                            $data[] = $insert;
                        }
                    }else{
                        return json(['code' => 100,'msg'=>"{$name}人员的手机号错误"]);
                    }
                }
            }
            if($data){
                $res = Db::table('up_u_user')->insertAll($data);
                if($res){
                    return json(['code' => 200,'msg'=>"导入成功"]);
                }else{
                    return json(['code' => 100,'msg'=>"导入失败"]);
                }
            }else{
                return json(['code' => 100,'msg'=>"有效数据为空"]);
            }
        }
        return $this->fetch();
    }
    /**
     * 公司信息导出Excel
     * @return \think\response\Json
     */
    public function outExcelCom(){
        extract(input());
        set_time_limit(0);//防止超时
        $where = [
            'c_status' => ['<>',3]
        ];
        if(isset($user_name) && $user_name != ''){
            $where['c_name'] = ['like', '%' . $user_name . '%'];
        }
        if(isset($c_verify) && $c_verify != ''){
            $where['c_verify'] = $c_verify;
        }
        if(isset($c_status) && $c_status != ''){
            $where['c_status'] = $c_status;
        }
        if($ids){
            if($ids != 'all'){
                $where['c_id'] = ['in',$ids];
            }
        }else{
            return json(['code'=>0,'msg'=>'请选择数据！']);
        }
        $field = [
            'c_id','c_name','c_logo','c_charge_name', 'c_charge_phone',
            'c_faren','c_area_str','c_address','c_verify','c_status',
            'c_time'
        ];
        $data = Db::table('up_company')
            ->where($where)
            ->order('c_status asc,c_time desc')
            ->field($field)
            ->select();
        if($data){
            $cellname = [
                '企业ID', '企业名称', '企业logo地址', '负责人', '负责人电话',
                '企业法人', '企业省市区', '地址详情', '认证状态', '企业状态',
                '创建时间'
            ];
            $status = ['c_verify','c_status'];
            $values = [
                'c_verify' => [1 => '认证中',2 => '已通过',3 => '已拒绝'],
                'c_status' => [1 => '正常',2 => '冻结']
            ];
            $res = exportExcel('companydata',time(),$cellname,$data,$status,$values);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
    /**
     * 团队信息导出Excel
     * @return \think\response\Json
     */
    public function outExcelTeam(){
        extract(input());
        set_time_limit(0);//防止超时
        $where['ct_c_id'] = session('c_id');
        $where['ct_status'] = ['<>',3];
        if(isset($user_name) && $user_name != ''){
            $where['ct_name|ct_charge_name|ct_charge_phone'] = ['like', '%' . $user_name . '%'];
        }
        if($ids){
            if($ids != 'all'){
                $where['ct_id'] = ['in',$ids];
            }
        }else{
            return json(['code'=>0,'msg'=>'请选择数据！']);
        }
        $field = [
            'ct_id','ct_name','ct_id users','ct_charge_name','ct_charge_phone',
            'ct_time','ct_update','ct_status','c_name'
        ];
        $data = Db::table('up_company_team ct')
            ->join('up_company c','ct.ct_c_id = c.c_id','left')
            ->where($where)
            ->order('ct_status asc,ct_update desc')
            ->field($field)
            ->select();
        if($data){
            foreach ($data as $k=>$v){
                $data[$k]['users'] = Db::table('up_u_user')
                    ->where('user_type',2)
                    ->where('user_room',$v['ct_id'])
                    ->where('user_status','<',3)
                    ->count();
            }
        }
        if($data){
            $cellname = [
                '团队ID', '团队名称', '团队人数', '团队负责人', '负责人电话',
                '创建时间', '最近修改时间', '团队状态', '企业名称'
            ];
            $status = ['ct_status'];
            $values = [
                'ct_status' => [1 => '正常',2 => '冻结']
            ];
            $res = exportExcel('teamdata',time(),$cellname,$data,$status,$values);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
    /**
     * 团队人数信息导出Excel
     * @return \think\response\Json
     */
    public function outExcelUser(){
        extract(input());
        set_time_limit(0);//防止超时
        $where['user_room'] = session('ct_id');
        $where['user_type'] = 2;
        $where['user_status'] = ['<',3];
        if(isset($user_name) && $user_name != ''){
            $where['user_username|user_phone'] = ['like', '%' . $user_name . '%'];
        }
        if(isset($user_status) && $user_status != ''){
            $where['user_status'] = $user_status;
        }
        if($ids){
            if($ids != 'all'){
                $where['user_id'] = ['in',$ids];
            }
        }else{
            return json(['code'=>0,'msg'=>'请选择数据！']);
        }
        $field = [
            'user_id','user_username','user_sex','user_birthday','user_job_desc',
            'user_phone','user_auth','user_time','user_status','c_name',
            'ct_name'
        ];
        $data = Db::table('up_u_user u')
            ->join('up_company c','u.user_hospital = c.c_id','left')
            ->join('up_company_team ct','u.user_room = ct.ct_id','left')
            ->where($where)
            ->order('user_status asc,user_updatetime desc')
            ->field($field)
            ->select();
        if($data){
            $cellname = [
                '用户ID', '用户名称', '性别', '生日', '介绍',
                '手机号', '用户角色', '加入时间', '用户状态', '所属企业',
                '所属团队'
            ];
            $status = ['user_sex','user_auth','user_status'];
            $values = [
                'user_sex' => [1 => '男',2 => '女'],
                'user_auth' => [1 => '业务员',2 => '区域负责人'],
                'user_status' => [1 => '正常',2 => '冻结']
            ];
            $res = exportExcel('userdata',time(),$cellname,$data,$status,$values);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
}